package com.winchain.chainbill.bill.biz.receive.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.service.CbRcvService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.receive.cmd.BillRcvCmd;
import com.winchain.chainbill.bill.biz.receive.vo.BillRcv;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.chain.callback.CallbackService;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import com.winchain.chainbill.trade.cmd.TranRollbackCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billAprvRjctCallbackService")
public class BillAprvRjctCallbackServiceImpl implements CallbackService {
	@Autowired
	private CbRcvService cbRcvService;
	@Autowired
	private BillOptCmd<CbRcv> billOptCmd;
	@Autowired
	private BillMoveCmd<CbRcv> billMoveCmd;
	@Autowired
	private BillRcvCmd billInputCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbRcv>> tranCallbackCmd;
	@Autowired
	private TranRollbackCmd<Bill<CbRcv>> tranRollbackCmd;

	public String getType() {
		return "rc_aprv_rj";
	}

	public void confirm(AcctTxRec tran) {
		String inputId = tran.getBizId();
		CbRcv cbRcv = (CbRcv) this.cbRcvService.selectByKey(inputId);

		BillRcv billInput = new BillRcv();
		billInput.setBillId(cbRcv.getBillId());
		billInput.setRcvState("1");
		BillRcv record = this.cbRcvService.selectBillRcv(billInput);

		record.setRcvState("-1");

		Bill<CbRcv> bill = new Bill();
		bill.setRec(record.getRec());
		bill.setSource(record.getRcv());

		CbOpt opt = new CbOpt();
		opt.setCurState("S034");
		opt.setOptCode("EX11");
		opt.setRemark("����������������������������");

		CallbackContext<Bill<CbRcv>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		context.setValue("bizType", "rc_aprv_rj");

		ChainCallback<Bill<CbRcv>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billMoveCmd);
		ccb.add(this.billInputCmd);
		ccb.add(this.tranCallbackCmd);
		ccb.callback(context);
	}

	public void cancel(AcctTxRec tran) {
		String inputId = tran.getBizId();
		CbRcv cbRcv = (CbRcv) this.cbRcvService.selectByKey(inputId);
		BillRcv billInput = new BillRcv();
		billInput.setBillId(cbRcv.getBillId());
		BillRcv record = this.cbRcvService.selectBillRcv(billInput);
		Bill<CbRcv> bill = new Bill();
		bill.setRec(record.getRec());
		bill.setSource(record.getRcv());

		CbOpt opt = new CbOpt();
		opt.setCurState("S034");
		opt.setOptCode("EX12");

		opt.setRemark("����������������������������");

		CallbackContext<Bill<CbRcv>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		ChainCallback<Bill<CbRcv>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billMoveCmd);
		ccb.add(this.billInputCmd);
		ccb.add(this.tranRollbackCmd);
		ccb.callback(context);
	}
}
